public class BinarySearchTree {

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode root = null;

    /**
     * 寻找一个元素
     * @param val
     * @return
     */
    public TreeNode find(int val) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val > val) {
                cur = cur.left;
            }else if (cur.val < val){
                cur = cur.right;
            }else {
                return cur;
            }
        }
        return null;
    }

    /**
     * 插入一个元素
     * @param val
     */
    public void insert(int val) {
        if (root == null) {
            root = new TreeNode(val);
        }
        TreeNode cur = root;
        TreeNode prev = null;
        while (cur != null) {
            if (val < cur.val) {
                prev = cur;
                cur = cur.left;
            }else if (val > cur.val) {
                prev = cur;
                cur = cur.right;
            }else {
                return;
            }
        }
        if (val > prev.val) {
            prev.right = new TreeNode(val);
        }else  {
            prev.left = new TreeNode(val);;
        }

    }

    /**
     * 中序遍历
     * @param root
     */
    public void inorder(TreeNode root) {
        if (root == null) {
            return;
        }
        inorder(root.left);
        System.out.print(root.val +" ");
        inorder(root.right);
    }

    /**
     * 删除一个值
     * @param val
     */
    public void remove(int val) {
        if (root == null) {
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else if (cur.val < val) {
                parent = cur;
                cur = cur.right;
            }else {
                removeChild(parent,cur);
                return;
            }
        }
    }

    private void removeChild(TreeNode parent, TreeNode cur) {
        if (cur.left == null) {
            if (cur == root) {
                root = root.right;
            } else if (parent.left == cur) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if (cur == root) {
                root = root.left;
            }else if (parent.right == cur) {
                parent.right = cur.left;
            }else{
                parent.left = cur.left;
            }
        }else {
            TreeNode targetP = cur;
            TreeNode target = cur.right;
            while (target.left != null) {
                targetP = target;
                target = target.left;
            }
            cur.val = target.val;
            if (target == targetP.left) {
                targetP.left = target.right;
            }else {
                targetP.right = target.right;
            }
        }
    }

}
